﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;
using System.Globalization;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PR_PromotionDept : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        int EmpSeq;
        RadComboBox cmbJobSite;


        public void IncludeJava()
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", "../../Scripts/jsfunction.js");
            Page.Header.Controls.Add(script);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            IncludeJava();
            if (!IsPostBack)
            {
                EmpSeq = ssMng.EmpSeqI;// ssMng.EmpSeqI;
                
                
                //this.lblDate.Text = DateTime.Today.ToString();
                GetDepartName();
                //bindEmpType();
                bindEmpType1();
                //bindYearCombo();
                bindYearCombo1();
                bindJobSite();
                LoadDataGrid();
            }
            else
            {
                //this.cmbEmployeeType = (RadComboBox)EmployeeTypeCombo.FindControl("cbEmployeeType");
                //this.cmbyear = (RadComboBox)yearCombo.FindControl("cbYear");
                this.cmbJobSite = (RadComboBox)JobSiteCombo.FindControl("cbJobSite");
                EmpSeq = ssMng.EmpSeqI;
            }
            
        }

        private void SelectedJobSite()
        {
            //PromotionDeptBLL obj = new PromotionDeptBLL();
            //RadComboBox TmpJobSiteCombo = (RadComboBox)JobSiteCombo.FindControl("cbType");
        }

        private void GetDepartName()
        {
            PromotionDeptBLL obj = new PromotionDeptBLL();
            lblDepName.Text = obj.GetDepartName(this.EmpSeq);
        }
        
        private void LoadDataGrid()
        {
            try
            {
                PromotionDeptBLL obj = new PromotionDeptBLL();

                DataTable dt;

                dt = obj.select();

                RadGrid2.DataSource = dt;
                RadGrid2.DataBind();
            }
            catch
            {
                
            }
        }

        private void LoadDataGridOverLoad()
        {
            try
            {
                PromotionDeptBLL obj = new PromotionDeptBLL();
                int PrmSeq = obj.GetPrmPromotionSeq(Int32.Parse(cbEmployeeType.SelectedValue.ToString()),
                    Int32.Parse(cbYear.SelectedValue.ToString()), Int32.Parse(radPeriod.SelectedValue.ToString()));
                DataTable dt;
                dt = obj.select(Convert.ToInt32(cbYear.SelectedValue.ToString())
                        , PrmSeq, Convert.ToInt32(cbEmployeeType.SelectedValue.ToString())
                        , Convert.ToInt32(cmbJobSite.SelectedValue.ToString()));

                RadGrid2.DataSource = dt;
                RadGrid2.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
                RadGrid2.DataBind();
            }
            catch
            {
                LoadDataGrid();
                RadGrid2.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None;

                RadGrid2.DataBind();
            }
        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            //Session["id"] = cbEmployeeType.SelectedValue.ToString();
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["prm_emp_seq"]);
                PromotionDeptBLL obj = new PromotionDeptBLL();
                obj.Delete(seq, 1);
                // salaryCommandBLL.Delete(seq, ssMng.EmpSeq);
            }
            if (e.CommandName == "btnClick")
            {
                return;
            }
            if (e.CommandName == "InitInsert")
            {
                this.cmbJobSite = (RadComboBox)JobSiteCombo.FindControl("cbJobSite");
                Session["JSID"] = this.cmbJobSite.SelectedValue.ToString();
                Session["ETID"] = cbEmployeeType.SelectedValue.ToString();
                Session["PrmYear"] = Int32.Parse(cbYear.SelectedValue.ToString());
                Session["PrmPriod"] = Int32.Parse(radPeriod.SelectedValue.ToString());
            }
            LoadDataGridOverLoad();
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
         

        }

        //private void bindEmpType()
        //{
        //    this.cmbEmployeeType = (RadComboBox)EmployeeTypeCombo.FindControl("cbEmployeeType");
        //    //EmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        //    EmployeeTypeCombo.ListEmployeeTypeComboBox();
        //    this.cmbEmployeeType.AutoPostBack = true;
        //    //this.cmbEmployeeType.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(cbEmployeeType_SelectedIndexChanged);
        //}

        private void bindEmpType1()
        {
            PromotionDeptBLL obj = new PromotionDeptBLL();
            cbEmployeeType.DataSource = obj.GetEmployeeTypes();
            cbEmployeeType.DataTextField = "EMP_TYPE_NAME";
            cbEmployeeType.DataValueField = "EMP_TYPE_SEQ";
            cbEmployeeType.DataBind();
            cbEmployeeType.SelectedIndex = 0;
            cbEmployeeType_SelectedIndexChanged(null, null);
        }

        //private void bindYearCombo()
        //{
        //    int curYear, min, max;
        //    this.cmbyear = (RadComboBox)yearCombo.FindControl("cbYear");
        //    curYear = int.Parse(DateTime.Today.Year.ToString());
        //    curYear += 543;
        //    min = curYear - 10;
        //    max = curYear + 10;
        //    yearCombo.ListYear(min, max);
        //    this.cmbyear.SelectedValue = curYear.ToString();
        //}

        public void bindYearCombo1()
        {
            int curYear = int.Parse(DateTime.Today.Year.ToString());
            curYear += 543;
            int MinYear = curYear - 10;
            int MaxYear = curYear + 10;
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            cbYear.DataSource = dt;
            cbYear.DataBind();
            cbYear.SelectedValue = curYear.ToString();
            cbYear_SelectedIndexChanged(null, null);
        }

        private void bindJobSite()
        {
            cmbJobSite = (RadComboBox)JobSiteCombo.FindControl("cbJobSite");
            JobSiteCombo.ListJobSiteComboBox("2");
            PromotionDeptBLL obj = new PromotionDeptBLL();
            cmbJobSite.SelectedValue = obj.GetJobSiteSeq(this.EmpSeq).ToString();
            JobSiteCombo.DataBind();
        }

        protected void cbEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            PromotionDeptBLL obj = new PromotionDeptBLL();
            lblTypeYear.Text = obj.GetYearTypeName(Convert.ToInt32(cbEmployeeType.SelectedValue)) + " : ";
            
        }

        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            if (cbYear.SelectedIndex == -1)
            {
                return;
            }
            if (cbEmployeeType.SelectedIndex == -1)
            {
                return;
            }
            LoadDataGridOverLoad();
        }

        protected void cbYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cbYear.SelectedIndex == -1)
            {
                radPeriod.Items.Clear();
                return;
            }
            PromotionDeptBLL obj = new PromotionDeptBLL();
            DataTable dt = obj.GetAppraisals(Int32.Parse(cbYear.SelectedValue.ToString()));
            if (dt != null && dt.Rows.Count > 0)
            {
                radPeriod.Items.Clear();
                foreach(DataRow cur in dt.Rows)
                {
                    ListItem Li = new ListItem();
                    Li.Text = cur["apr_name"].ToString() + " " + ((DateTime)cur["act_start_date"]).ToString("dd/MM/yyyy",new CultureInfo("th-TH")) + " ถึง " + ((DateTime)cur["act_end_date"]).ToString("dd/MM/yyyy",new CultureInfo("th-TH"));
                    Li.Value = cur["apr_period"].ToString();
                    radPeriod.Items.Add(Li);
                }
                //radPeriod.DataSource = dt;
                //radPeriod.DataTextField = "apr_name";
                //radPeriod.DataValueField = "apr_period";
                radPeriod.DataBind();
                radPeriod.SelectedIndex = 0;
            }
            else
            {
                radPeriod.Items.Clear();
            }
        }


    }
}
